package desktop.agent;

import java.util.List;
import org.xmlsoap.schemas.ws._2003._03.addressing.EndpointReferenceType;
import com.nortel.soa.oi.cct.basenotification.AgentPropertyEventType;
import com.nortel.soa.oi.cct.basenotification.NotificationEventHolderType;
import com.nortel.soa.oi.cct.basenotification.NotificationMessageHolderType;
import com.nortel.soa.oi.cct.notificationconsumer.NotificationConsumer;
import com.nortel.soa.oi.cct.types.AgentLoginStatus;
import com.nortel.soa.oi.cct.types.AgentProperty;
import com.nortel.soa.oi.cct.types.AgentPropertyLoginStatus;
import com.nortel.soa.oi.cct.types.AgentPropertyReadyStatus;
import com.nortel.soa.oi.cct.types.Contact;

@javax.jws.WebService(name = "NotificationConsumer",
		serviceName = "SOAOICCT_NotificationConsumer",
		portName = "NotificationConsumer",
		targetNamespace = "http://www.nortel.com/soa/oi/cct/NotificationConsumer",
		endpointInterface = "com.nortel.soa.oi.cct.notificationconsumer.NotificationConsumer")

public class NotificationConsumerImpl implements NotificationConsumer
{

	private boolean ringing = false;
	private boolean answered = false;
	private  ServiceUtil serviceUtil;

	public  void setServiceUtil(ServiceUtil serviceUtil) {
		this.serviceUtil = serviceUtil;
	}

	private OICCADNotificationListener notificationListener;
	private EndpointReferenceType subscriptionEndpoint;

	public void setNotificationListener(OICCADNotificationListener notificationListener) {
		this.notificationListener = notificationListener;
	}

 

	@Override
	public synchronized void notify(List<NotificationMessageHolderType> notificationMessage) {

		String subId = null;
		if (subscriptionEndpoint != null) {
			subId = subscriptionEndpoint.getSubscriptionId();
		}


		try {
			for(NotificationMessageHolderType nmh : notificationMessage) {
				NotificationEventHolderType notificationEventHolder = nmh.getMessage();

				if (nmh.getSubscriptionId() != null && nmh.getSubscriptionId().equals(subId)) {

					System.err.println("Notification = " + notificationEventHolder.getEventType().name());



					// JOptionPane.showMessageDialog(null,"Notifications To Handle..." + notificationEventHolder.getEventType().name());

					switch (notificationEventHolder.getEventType()) {
					case ADDRESS_PROPERTY:
						//handle Address Property notification here
						break;
					case ADDRESS_STATE:
						//handle Address Property notification here
						break;
					case AGENT_PROPERTY:
						handleAgentPropertyNotification(notificationEventHolder);
						break;
					case AGENT_STATE:
						//handleAgentStateNotification(notificationEventHolder);
						break;
					case CONNECTION_STATE:
						//handle Address Property notification here
						break;
					case CONTACT_PROPERTY:
						//handle contact Property notification here
						break;
					case DEFAULT_TREATMENT:    
						//handle default treatment notification here
						break;
					case FORCE_DISCONNECT:
						//handle force disconnect notification here
						break;
					case SERVICE_PROVIDER_STATUS:
						//handle Service Provider Status notification here
						break;
					case SYSTEM:
						//handle System notification here
						break;
					case TERMINAL_CONNECTION_STATE:
						handleTerminalConnectionNotification(notificationEventHolder);
						break;
					case TERMINAL_PROPERTY:
						//handle Terminal Property notification here
						break;
					case TERMINAL_STATE:
						handleTerminalStateNotification(notificationEventHolder);
						break;


					}
				} else {
					System.err.println("Notification received for sub id = " + nmh.getSubscriptionId());
				}

				Thread.sleep(1000);

			}

		} catch (Exception ex) {
			ex.printStackTrace();
			//throw new RuntimeException(ex);
		}
	}

	private void handleTerminalStateNotification(
			NotificationEventHolderType notificationEventHolder) {
		// TODO Auto-generated method stub

	}



	private void handleAgentPropertyNotification(
			NotificationEventHolderType notificationEventHolder) {
		AgentPropertyEventType agentPropEvent = notificationEventHolder.getAgentPropertyEvent();


		AgentProperty agentProp = agentPropEvent.getAgentProperty();
		agentProp.name();

		switch (agentProp) {
		
			case READY_STATUS :
				AgentPropertyReadyStatus prop = agentPropEvent.getAgentPropertyReadyStatus();
				if (prop != null && prop.isStatus()) {
					this.notificationListener.actionPerformed("AGENT_READY");
				} else {
					this.notificationListener.actionPerformed("AGENT_NOT_READY");
				}
				break;
				
			case LOGIN_STATUS :
				AgentPropertyLoginStatus loginProp = agentPropEvent.getAgentPropertyLoginStatus();
				AgentLoginStatus agentStatus = loginProp.getStatus();
				if (agentStatus == AgentLoginStatus.LOGGED_IN) {
					this.notificationListener.actionPerformed("LOGGED_IN");
				} else {
					this.notificationListener.actionPerformed("LOGGED_OUT");
				}
				break;
		
			}

	}

	 

	private void handleTerminalConnectionNotification(
			NotificationEventHolderType notificationEventHolder) {
		System.err.println("TERMINAL_CONNECTION_STATE  = "  + notificationEventHolder.getTerminalConnectionStateEvent().getNewState());


		try{
			String state = notificationEventHolder.getTerminalConnectionStateEvent().getNewState().value();
			System.err.println("TERMINAL_CONNECTION_STATE  = " + state);
			Contact contact = notificationEventHolder.getTerminalConnectionStateEvent().getContact();
			//RINGING
			//  ACTIVE
			//  DROPPED
			serviceUtil.setContact(contact);

			if(state.equalsIgnoreCase("RINGING") && !ringing){
				this.ringing = true;
				this.answered = false;
				this.notificationListener.actionPerformed(state);

			}

			if(state.equalsIgnoreCase("ACTIVE") && !answered){
				this.ringing = false;
				this.answered = true;
				this.notificationListener.actionPerformed(state);
			}

			if(state.equalsIgnoreCase("DROPPED")){
				this.ringing = false;
				this.answered = false;
				this.notificationListener.actionPerformed(state);
			}

		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	public void setSubscriptionReference(
			EndpointReferenceType subscriptionEndpoint) {
		this.subscriptionEndpoint = subscriptionEndpoint;

	}



}
